package com.rpp.study.utils;

import java.util.Random;


/**
 * @Description
 * @Author rpp
 * @Date 2020/8/28 11:30 下午
 */
public class RandomStringGenerator {


    private static final String CODE_NUM_STR = "abcdefghijklmnopqrstuvwxyz0123456789";

    private static final String NUM_STR = "0123456789";

    /**
     * 获取一定长度的随机字符串
     *
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomString(int length) {
        return getRandom(CODE_NUM_STR, length);
    }

    /**
     * 获取一定长度的随机数字
     *
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomNumber(int length) {
        return getRandom(NUM_STR, length);
    }


    private static String getRandom(String base, int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}
